package com.lianwei.hospital.controller;

import com.lianwei.hospital.Service.Impl.OpdInfoImpl;
import com.lianwei.hospital.Service.OpdInfoService;
import com.lianwei.hospital.module.entity.OpdInfo;
import com.lianwei.hospital.module.vo.OpdVO;
import com.lianwei.hospital.utils.PageUtil;
import com.lianwei.hospital.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/opd")
public class OpdInfoController {
    @Autowired
    private OpdInfoService opdInfoService;

    @GetMapping("/selectById")
    public Result<OpdInfo> Select(String i) throws Exception {
        System.out.println(i+"\n\n");
        OpdInfo opdInfo = opdInfoService.selectByPrimaryKey(i);
        return Result.buildResult(Result.Status.OK,opdInfo);
    }

    @PutMapping
    public Result<List<OpdInfo>> Insert(@RequestBody OpdInfo opdInfo) throws Exception {
        System.out.println(opdInfo);
        if(opdInfoService.insert(opdInfo)){
            List<OpdInfo> opdInfoList = opdInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,opdInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @DeleteMapping
    public Result<List<OpdInfo>> Delete(@RequestBody OpdInfo opdInfo) throws Exception {
        System.out.println(opdInfo);
        if(opdInfoService.deleteByPrimaryKey(String.valueOf(opdInfo.getId()))){
            List<OpdInfo> opdInfoList = opdInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,opdInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PostMapping
    public Result<List<OpdInfo>> Update(@RequestBody OpdInfo opdInfo) throws Exception {
        System.out.println(opdInfo);
        if(opdInfoService.updateByPrimaryKey(opdInfo)){
            List<OpdInfo> opdInfoList = opdInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,opdInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectAll")
    public Result<PageUtil<OpdInfo>> list(@RequestBody OpdVO query) throws Exception {
        PageUtil<OpdInfo> pageUtil=opdInfoService.findPage(query);
        return Result.buildResult(Result.Status.OK,pageUtil);
    }


}
